package com.longjianghuasheng.logistics.business.order.fragment

import android.annotation.SuppressLint
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
import android.view.inputmethod.EditorInfo
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseFragment
import com.longjianghuasheng.logistics.business.order.viewmodel.OrderViewModel
import com.longjianghuasheng.logistics.databinding.FragmentOrderOutBinding
import com.longjianghuasheng.logistics.entity.OrderType
import com.longjianghuasheng.logistics.eventViewModel
import com.longjianghuasheng.logistics.ext.observer


/**
 * 订单
 */
class OrderOutFragment :
    BaseFragment<OrderViewModel, FragmentOrderOutBinding>() {


    private val fragments by lazy {
        arrayListOf<Fragment>()
    }
    private val pagerAdapter by lazy {
        OrderOutPagerAdapter(this)
    }


    override fun layoutId(): Int = R.layout.fragment_order_out

    override fun initView(savedInstanceState: Bundle?) {

        mDatabind.editSearch.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                eventViewModel.orderSearchEvent.value = v.text.toString()
                return@setOnEditorActionListener true
            }

            false
        }
        initTitle()

    }

    private fun FragmentOrderOutBinding.initTabAndViewPager(arrayList: ArrayList<OrderType>) {


        arrayList.forEachIndexed { index, orderType ->

            tabLayout.addTab(tabLayout.newTab().setText(orderType.title))
            fragments.add(OrderInFragment.newInstance(orderType.id ?: 0, index))

        }


        viewPager.run {


            offscreenPageLimit = arrayList.size

            adapter = pagerAdapter


            isUserInputEnabled = false



            TabLayoutMediator(tabLayout, this) { tab, position ->
                tab.text = arrayList[position].title


            }.attach()

        }

        tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {


                viewPager.setCurrentItem(tab?.position ?: 0, false)
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }
        })

        tabLayout.getTabAt(0)
            ?.select()


    }

    @SuppressLint("NewApi", "UseCompatTextViewDrawableApis")
    private fun initTitle() {
        mDatabind.include.titleBar.run {

            leftText.compoundDrawableTintList = ColorStateList.valueOf(Color.WHITE)

            setTitle("我的订单").centerText.typeface = Typeface.defaultFromStyle(Typeface.BOLD)

            setLeftClickListener {
                navigateUp()
            }
        }


    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.serviceOrderType.observer(this) {

            mDatabind.initTabAndViewPager(it)
        }
    }

    override fun initData() {
        super.initData()
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        mViewModel.service_order_type()
    }


    inner class OrderOutPagerAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {


        override fun getItemCount(): Int = fragments.size

        override fun createFragment(position: Int): Fragment = fragments[position]

    }
}